import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:google_fonts/google_fonts.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';
import 'package:thal/backstage/toolkit/utilities/app_constants.dart';
import 'package:thal/backstage/stagehand/app_routes.dart';
import 'package:thal/backstage/control_room/user_room.dart';
import 'package:thal/backstage/control_room/check_in_room.dart';
import 'package:thal/backstage/control_room/follow_room.dart';
import 'package:thal/backstage/control_room/lake_recommend_room.dart';
import 'package:thal/backstage/control_room/dynamic_room.dart';
import 'package:thal/backstage/control_room/badge_room.dart';
import 'package:thal/backstage/control_room/group_chat_room.dart';
import 'package:thal/backstage/control_room/single_chat_room.dart';
import 'package:thal/backstage/control_room/quiz_room.dart';
import 'package:thal/backstage/control_room/ai_chat_room.dart';
import 'package:thal/backstage/control_room/global_state_provider.dart';
import 'package:thal/backstage/toolkit/widgets/loading_indicator.dart';

void main() {
  // 初始化 EasyLoading
  LoadingCurtain.bootstrap();
  runApp(const MeshApp());
}

class MeshApp extends StatefulWidget {
  const MeshApp({super.key});

  @override
  State<MeshApp> createState() => _MeshAppState();
}

class _MeshAppState extends State<MeshApp> {
  // 将状态实例声明为类的成员变量，确保在整个widget生命周期内只创建一次
  late final UserRoom userRoom;
  late final CheckInRoom checkInRoom;
  late final FollowRoom followRoom;
  late final LakeRecommendRoom lakeRecommendRoom;
  late final DynamicRoom dynamicRoom;
  late final BadgeRoom badgeRoom;
  late final GroupChatRoom groupChatRoom;
  late final SingleChatRoom singleChatRoom;
  late final QuizRoom quizRoom;
  late final AiChatRoom aiChatRoom;

  @override
  void initState() {
    super.initState();
    // 在initState中只创建一次状态管理实例
    userRoom = UserRoom();
    checkInRoom = CheckInRoom();
    followRoom = FollowRoom();
    lakeRecommendRoom = LakeRecommendRoom();
    dynamicRoom = DynamicRoom();
    badgeRoom = BadgeRoom();
    groupChatRoom = GroupChatRoom();
    singleChatRoom = SingleChatRoom();
    quizRoom = QuizRoom();
    aiChatRoom = AiChatRoom();

    // 初始化所有默认数据
    userRoom.initializeDefaultUsers();
    checkInRoom.initializeDefaultCheckIns();
    followRoom.initializeDefaultFollows();
    lakeRecommendRoom.initializeDefaultLakeRecommends();
    dynamicRoom.initializeDefaultDynamics();
    badgeRoom.initializeDefaultBadges();
    groupChatRoom.initializeDefaultGroupChats();
    singleChatRoom.initializeDefaultSingleChats();
    quizRoom.initializeDefaultQuizQuestions();
    aiChatRoom.initSession();
  }

  @override
  Widget build(BuildContext context) {
    return ScreenUtilInit(
      minTextAdapt: true,
      splitScreenMode: true,
      designSize: const Size(390, 844),
      builder: (context, child) {
        return GlobalStateProvider(
          userRoom: userRoom,
          checkInRoom: checkInRoom,
          followRoom: followRoom,
          lakeRecommendRoom: lakeRecommendRoom,
          dynamicRoom: dynamicRoom,
          badgeRoom: badgeRoom,
          groupChatRoom: groupChatRoom,
          singleChatRoom: singleChatRoom,
          quizRoom: quizRoom,
          aiChatRoom: aiChatRoom,
          child: MaterialApp.router(
            title: ProjectConstants.appName,
            debugShowCheckedModeBanner: false,
            // 配置katana_router
            routerConfig: appRouter,
            theme: ThemeData(
              textButtonTheme: TextButtonThemeData(
                style: TextButton.styleFrom(
                  textStyle: TextStyle(decoration: TextDecoration.none),
                ),
              ),
              primarySwatch: Colors.yellow,
              splashColor: Colors.transparent,
              highlightColor: Colors.transparent,
              fontFamily: GoogleFonts.inter().fontFamily,
            ),
            // 初始化EasyLoading
            builder: EasyLoading.init(),
          ),
        );
      },
    );
  }
}
